﻿using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Web;
using System.IO;
using Twitterizer.Framework;
using System.Windows.Media.Imaging;

namespace TwitterApp
{
    namespace BusinessLogic
    {
        public class Status
        {
            public string Message
            {
                get;
                set;
            }

            public string UserName
            {
                get;
                set;
            }

            public string ScreenName
            {
                get;
                set;
            }

            public BitmapImage Avatar
            {
                get;
                set;
            }

            public Status()
            {

            }

            public void Load(TwitterStatus twitterstatus)
            {
                this.Message = twitterstatus.Text;
                this.UserName = twitterstatus.TwitterUser.UserName;
                this.ScreenName = twitterstatus.TwitterUser.ScreenName;
                this.Avatar = LoadImage(twitterstatus.TwitterUser.ProfileImageUri.AbsoluteUri);
            }

            private BitmapImage LoadImage(string imageURL)
            {
                BitmapImage avatarbitmap = new BitmapImage();

                try
                {
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(imageURL);
                    request.Timeout = 5000; // 5 seconds in milliseconds
                    request.ReadWriteTimeout = 20000; // allow up to 20 seconds to elapse

                    // execute the request
                    HttpWebResponse response = (HttpWebResponse)
                    request.GetResponse();

                    avatarbitmap.BeginInit();
                    avatarbitmap.StreamSource = response.GetResponseStream();
                    avatarbitmap.EndInit();
                }
                catch (Exception)
                {
                    avatarbitmap = null;
                }

                return avatarbitmap;
            }
        }
    }
}